#include "Record.h"
#include "ArbRecord.h"
#include "Node.h"
#include "RecordList.h"
#include "Diskpage.h"
#include <stdlib.h>
#include <vcclr.h>
#include <string>
#include <cstring>

#pragma once


namespace Project {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::TextBox^  txtRawFileName;
	protected: 

	private: System::Windows::Forms::Button^  btnRawFile;


	protected: 

	protected: 

	protected: 

	protected: 

	private: System::Windows::Forms::OpenFileDialog^  openFileDialog;
























	private: System::Windows::Forms::NumericUpDown^  numericRecord;

	private: System::Windows::Forms::Button^  btnFindRecord;
	private: System::Windows::Forms::Button^  btnAsciiFile;
	private: System::Windows::Forms::TextBox^  txtAsciiFileName;


	private: RecordList *recordList;
	private: System::Windows::Forms::RichTextBox^  output;







	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->txtRawFileName = (gcnew System::Windows::Forms::TextBox());
			this->btnRawFile = (gcnew System::Windows::Forms::Button());
			this->openFileDialog = (gcnew System::Windows::Forms::OpenFileDialog());
			this->numericRecord = (gcnew System::Windows::Forms::NumericUpDown());
			this->btnFindRecord = (gcnew System::Windows::Forms::Button());
			this->btnAsciiFile = (gcnew System::Windows::Forms::Button());
			this->txtAsciiFileName = (gcnew System::Windows::Forms::TextBox());
			this->output = (gcnew System::Windows::Forms::RichTextBox());
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->numericRecord))->BeginInit();
			this->SuspendLayout();
			// 
			// txtRawFileName
			// 
			this->txtRawFileName->Anchor = static_cast<System::Windows::Forms::AnchorStyles>(((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Left) 
				| System::Windows::Forms::AnchorStyles::Right));
			this->txtRawFileName->Location = System::Drawing::Point(12, 12);
			this->txtRawFileName->Name = L"txtRawFileName";
			this->txtRawFileName->ReadOnly = true;
			this->txtRawFileName->Size = System::Drawing::Size(295, 20);
			this->txtRawFileName->TabIndex = 0;
			// 
			// btnRawFile
			// 
			this->btnRawFile->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Right));
			this->btnRawFile->Location = System::Drawing::Point(313, 9);
			this->btnRawFile->Name = L"btnRawFile";
			this->btnRawFile->Size = System::Drawing::Size(75, 23);
			this->btnRawFile->TabIndex = 1;
			this->btnRawFile->Text = L"Raw File...";
			this->btnRawFile->UseVisualStyleBackColor = true;
			this->btnRawFile->Click += gcnew System::EventHandler(this, &Form1::OpenRawFile);
			// 
			// openFileDialog
			// 
			this->openFileDialog->DefaultExt = L"dat";
			this->openFileDialog->Filter = L"Data files|*.dat";
			this->openFileDialog->ValidateNames = false;
			// 
			// numericRecord
			// 
			this->numericRecord->Location = System::Drawing::Point(12, 67);
			this->numericRecord->Maximum = System::Decimal(gcnew cli::array< System::Int32 >(4) {1316134911, 2328, 0, 0});
			this->numericRecord->Minimum = System::Decimal(gcnew cli::array< System::Int32 >(4) {1, 0, 0, 0});
			this->numericRecord->Name = L"numericRecord";
			this->numericRecord->Size = System::Drawing::Size(71, 20);
			this->numericRecord->TabIndex = 4;
			this->numericRecord->Value = System::Decimal(gcnew cli::array< System::Int32 >(4) {1, 0, 0, 0});
			// 
			// btnFindRecord
			// 
			this->btnFindRecord->Location = System::Drawing::Point(12, 93);
			this->btnFindRecord->Name = L"btnFindRecord";
			this->btnFindRecord->Size = System::Drawing::Size(75, 23);
			this->btnFindRecord->TabIndex = 5;
			this->btnFindRecord->Text = L"Find Record";
			this->btnFindRecord->UseVisualStyleBackColor = true;
			this->btnFindRecord->Click += gcnew System::EventHandler(this, &Form1::FindRecord);
			// 
			// btnAsciiFile
			// 
			this->btnAsciiFile->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Right));
			this->btnAsciiFile->Location = System::Drawing::Point(313, 38);
			this->btnAsciiFile->Name = L"btnAsciiFile";
			this->btnAsciiFile->Size = System::Drawing::Size(75, 23);
			this->btnAsciiFile->TabIndex = 7;
			this->btnAsciiFile->Text = L"Ascii File...";
			this->btnAsciiFile->UseVisualStyleBackColor = true;
			this->btnAsciiFile->Click += gcnew System::EventHandler(this, &Form1::OpenAsciiFile);
			// 
			// txtAsciiFileName
			// 
			this->txtAsciiFileName->Anchor = static_cast<System::Windows::Forms::AnchorStyles>(((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Left) 
				| System::Windows::Forms::AnchorStyles::Right));
			this->txtAsciiFileName->Location = System::Drawing::Point(12, 41);
			this->txtAsciiFileName->Name = L"txtAsciiFileName";
			this->txtAsciiFileName->ReadOnly = true;
			this->txtAsciiFileName->Size = System::Drawing::Size(295, 20);
			this->txtAsciiFileName->TabIndex = 6;
			// 
			// output
			// 
			this->output->Location = System::Drawing::Point(89, 67);
			this->output->Name = L"output";
			this->output->Size = System::Drawing::Size(299, 198);
			this->output->TabIndex = 8;
			this->output->Text = L"";
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(400, 277);
			this->Controls->Add(this->output);
			this->Controls->Add(this->btnAsciiFile);
			this->Controls->Add(this->txtAsciiFileName);
			this->Controls->Add(this->btnFindRecord);
			this->Controls->Add(this->numericRecord);
			this->Controls->Add(this->btnRawFile);
			this->Controls->Add(this->txtRawFileName);
			this->Name = L"Form1";
			this->Text = L"CECS 335 - File Structures Team Project";
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->numericRecord))->EndInit();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion

private: System::Void FindRecord(System::Object^  sender, System::EventArgs^  e) {
			 if(this->openFileDialog->FileNames->Length > 0)
			 {
				 ArbRecord record = recordList->searchForRecord((int)this->numericRecord->Value);
				 this->output->Text = "Record " + this->numericRecord->Value.ToString() + "\n";
				 /*this->output->Text += record.getID().ToString() + "\n";
				 this->output->Text += gcnew String(record.getFirstName()) + "\n";
				 this->output->Text += record.getBalance().ToString() + "\n";
				 this->output->Text += record.getStreetNumber().ToString() + "\n";
				 this->output->Text += gcnew String(record.getFirstName()) + "\n";
				 this->output->Text += gcnew String(record.getLastName()) + "\n";
				 this->output->Text += gcnew String(record.getStreetName()) + "\n";
				 this->output->Text += gcnew String(record.getCity());*/
				 for(int i=0;i<recordList->getHeaderCount();i++)
				 {
					 String ^descrip = gcnew String((recordList->getHeaderNode(i)).getDescription());
					 descrip = descrip->Trim();
					 this->output->Text += descrip + " - ";
					 switch((record.getNode(i)).getDataType())
					 {
					 case 'A':
						 this->output->Text += (record.getNode(i)).getValueI().ToString();
						 break;
					 case 'B':
						 this->output->Text += (record.getNode(i)).getValueR().ToString();
						 break;
					 case 'C':
						 this->output->Text += (record.getNode(i)).getValueC().ToString();
						 break;
					 case 'D':
						 this->output->Text += gcnew String((record.getNode(i)).getValueS());
						 break;
					 default:
						 //
						 break;
					 }
					 this->output->Text += "\n";
				 }
			 }
		 }

private: System::Void OpenRawFile(System::Object^  sender, System::EventArgs^  e) {
			 this->openFileDialog->ShowDialog();
				 if(this->openFileDialog->FileNames->Length > 0)
					 {
						 this->txtRawFileName->Text = this->openFileDialog->FileName;

						 char* filename;

						 To_CharStar(this->openFileDialog->FileName, filename);

						 recordList = new RecordList(filename);

						 this->openFileDialog->FileNames->Clear(this->openFileDialog->FileNames, 0, 1);
					 }
		 }

private: System::Void OpenAsciiFile(System::Object^  sender, System::EventArgs^  e) {
			 this->openFileDialog->ShowDialog();
				 if(this->openFileDialog->FileNames->Length > 0)
					 {
						 this->txtAsciiFileName->Text = this->openFileDialog->FileName;

						 char* filename;

						 To_CharStar(this->openFileDialog->FileName, filename);

						 recordList->readInAsciiFile(filename, 0);
					 }
		 }

//This code was borrowed to help us change the System::String to a char*. Stupid char*.
bool To_CharStar( String^ source, char*& target )

{

    pin_ptr<const wchar_t> wch = PtrToStringChars( source );

    int len = (( source->Length+1) * 2);

    target = new char[ len ];

    return wcstombs( target, wch, len ) != -1;

}
};
}

